/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.pig4cloud.pigx.contract.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pigx.common.core.constant.SecurityConstants;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.log.annotation.SysLog;
import com.pig4cloud.pigx.contract.api.feign.ContractMarketRecordFeign;
import com.pig4cloud.pigx.contract.entity.ContractSymbol;
import com.pig4cloud.pigx.contract.service.ContractSymbolService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.pig4cloud.pigx.common.excel.annotation.ResponseExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 币对
 *
 * @date 2022-08-09 20:29:23
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/contractsymbol" )
@Api(value = "contractsymbol", tags = "币对管理")
public class ContractSymbolController {

    private final  ContractSymbolService contractSymbolService;

    private final ContractMarketRecordFeign feign;

    /**
     * 分页查询
     * @param page 分页对象
     * @param contractSymbol 币对
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('contract_contractsymbol_view')" )
    public R getContractSymbolPage(Page page, ContractSymbol contractSymbol) {
    	contractSymbol.setIsDeleted("0");
        return R.ok(contractSymbolService.page(page, Wrappers.query(contractSymbol)));
    }


    /**
     * 通过id查询币对
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('contract_contractsymbol_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(contractSymbolService.getById(id));
    }

	/**
	 * 获取币种列表
	 * @return R
	 */
	@ApiOperation(value = "获取币种列表", notes = "获取币种列表")
	@GetMapping("/currency" )
	@PreAuthorize("@pms.hasPermission('contract_contractsymbol_view')" )
	public R getCurrency() {
		return feign.contractList(SecurityConstants.FROM_IN);
	}

    /**
     * 新增币对
     * @param contractSymbol 币对
     * @return R
     */
    @ApiOperation(value = "新增币对", notes = "新增币对")
    @SysLog("新增币对" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('contract_contractsymbol_add')" )
    public R save(@RequestBody ContractSymbol contractSymbol) {
        return contractSymbolService.saveSymbol(contractSymbol);
    }

    /**
     * 修改币对
     * @param contractSymbol 币对
     * @return R
     */
    @ApiOperation(value = "修改币对", notes = "修改币对")
    @SysLog("修改币对" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('contract_contractsymbol_edit')" )
    public R updateById(@RequestBody ContractSymbol contractSymbol) {
        return contractSymbolService.updateSymbol(contractSymbol);
    }



    /**
     * 通过id删除币对
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除币对", notes = "通过id删除币对")
    @SysLog("通过id删除币对" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('contract_contractsymbol_del')" )
    public R removeById(@PathVariable Long id) {
        return R.ok(contractSymbolService.deleteSymbol(id));
    }


    /**
     * 导出excel 表格
     * @param contractSymbol 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('contract_contractsymbol_export')" )
    public List<ContractSymbol> export(ContractSymbol contractSymbol) {
        return contractSymbolService.list(Wrappers.query(contractSymbol));
    }

	/**
	 * 获取币对下拉选
	 * @return
	 */
	@ApiOperation(value = "获取币对下拉选", notes = "获取币对下拉选")
	@GetMapping("/option")
	public R getSymbolOption() {
		BaseMapper<ContractSymbol> mapper = contractSymbolService.getBaseMapper();
		QueryWrapper<ContractSymbol> wrapper = new QueryWrapper<>();
		wrapper.eq("is_deleted","0");
		wrapper.eq("status","1");
		wrapper.select("id,name,multiple");
		return R.ok(mapper.selectList(wrapper));
	}
}
